﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace OpenQuarters.WebQuarters.Core
{
    [global::System.AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public sealed class PropertyConfigurationAttribute : Attribute
    {
        private PropertyType propertyType = PropertyType.TextBox;

        public PropertyType PropertyType
        {
            get { return propertyType; }
            set { propertyType = value; }
        }

        private string optionSource = "";

        public string OptionSource
        {
            get { return optionSource; }
            set { optionSource = value; }
        }

        private string sourceField = "";

        public string SourceField
        {
            get { return sourceField; }
            set { sourceField = value; }
        }

        private int maxLength = 0;

        public int MaxLength
        {
            get { return maxLength; }
            set { maxLength = value; }
        }

        public PropertyConfigurationAttribute()
            : base()
        {
        }

        public PropertyConfigurationAttribute(PropertyType propertyType)
            : base()
        {
            this.PropertyType = propertyType;
        }

        public string GetEditControl(PropertyInfo Property)
        {
            if (PropertyType == PropertyType.CMSPage)
            {
                this.OptionSource = "OpenQuarters.WebQuarters.Core.CMSPage.OptionSource_Pages";
            }

            switch (PropertyType)
            {
                case PropertyType.TextArea:
                case PropertyType.HTML:
                    return @"<textarea name=""" + Property.Name + @"""></textarea>";
                case PropertyType.Password:
                    return @"<input type=""password"" name=""" + Property.Name + @"""" + (this.MaxLength != 0 ? @" maxlength=""" + this.MaxLength + @"""" : "") + " />";
                case PropertyType.CheckBox:
                    return @"<input type=""checkbox"" name=""" + Property.Name + @""" />";
                case PropertyType.DropDown:
                case PropertyType.CMSPage:
                    StringBuilder sbDropDown = new StringBuilder();
                    sbDropDown.Append(@"<select name=""" + Property.Name + @"""" + (this.OptionSource.Length > 0 ? @" rel=""" + this.OptionSource + @"""" : "") + @">");

                    if (this.OptionSource.Length == 0)
                    {
                        if (Property.PropertyType.IsEnum)
                        {
                            int i = 0;
                            foreach (var s in Enum.GetNames(Property.PropertyType))
                            {
                                sbDropDown.Append(@"<option id=""" + Guid.NewGuid().ToString() + @""" value=""" + i.ToString() + @""">" + s.Replace("_", " ") + @"</option>");
                                i++;
                            }
                        }
                    }

                    sbDropDown.Append(@"</select>");
                    return sbDropDown.ToString();
                case PropertyType.File:
                case PropertyType.Image:
                case PropertyType.Flash:
                case PropertyType.Video:
                    StringBuilder sbFile = new StringBuilder();
                    sbFile.Append(@"<input type=""text"" name=""" + Property.Name + @""" class=""filePath"" />");
                    string folder;
                    switch (PropertyType)
                    {
                        case PropertyType.Image:
                            folder = "images";
                            break;
                        case PropertyType.Flash:
                            folder = "flash";
                            break;
                        case PropertyType.Video:
                            folder = "video";
                            break;
                        default:
                            folder = "";
                            break;
                    }
                    sbFile.Append(@"<input type=""button"" name=""btn" + Property.Name + @""" class=""fileBrowse"" onClick=""cmsFilePicker_ShowPicker($(this).prev().attr('id'), '" + folder + @"');"" value=""Browse"" />");
                    return sbFile.ToString();
                case PropertyType.DatePicker:
                    return @"<input type=""text"" name=""" + Property.Name + @""" class=""datePicker"" />";
                case PropertyType.Hidden:
                    return @"<input type=""hidden"" name=""" + Property.Name + @""" />";
                default:
                    return @"<input type=""text"" name=""" + Property.Name + @"""" + (this.MaxLength != 0 ? @" maxlength=""" + this.MaxLength + @"""" : "") + " />";
            }
        }
    }
}
